package com.legolas.provider.api.serviceapi.activemq;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class TopicProducer {
    private static final String url = "tcp://127.0.0.1:61616";
    private static final String topicName = "provider1-topic-test";

    public void produce() throws JMSException {

        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);

        Connection connection = connectionFactory.createConnection();

        connection.start();

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //创建一个目标
        Destination destination = session.createTopic(topicName);
        //创建一个生产者
        MessageProducer producer = session.createProducer(destination);

        for (int i = 0; i < 20; i++) {
            TextMessage textMessage = session.createTextMessage("test" + i);
            producer.send(textMessage);
            System.out.println("TopicProducer发送消息" + textMessage.getText());
        }

        //关闭连接
        connection.close();
    }

}
